package com.example.demo.modules.customDefine.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.util.R;
import com.example.demo.modules.customDefine.entity.ModuleFieldDataJson;
import com.example.demo.modules.customDefine.service.ModuleFieldDataJsonService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/moduleFieldDataJson")
public class ModuleFieldDataJsonController {

    @Autowired
    private ModuleFieldDataJsonService moduleFieldDataJsonService;


    @GetMapping("/one")
    public R one(@RequestParam(value = "id", required = true) String id) {
        if(null == id || "".equals(id)){
            return R.error().put("msg", "缺少参数id");
        }

        return R.ok().put("result", moduleFieldDataJsonService.getById(id));
    }
    
    @GetMapping("/listAll")
    public R listAll() {
        return R.ok().put("result",moduleFieldDataJsonService.list(null));
    }

    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageSize",dataType="String"),
            @ApiImplicitParam(name="pageNum",dataType="String")
    })
    public R page(
            Integer pageSize,Integer pageNum) {
        if (null == pageSize || "".equals(pageSize)) {
            pageSize = 10;
        }
        if (null == pageNum || "".equals(pageNum)) {
            pageNum = 0;
        }
        QueryWrapper<ModuleFieldDataJson> queryWrapper = new QueryWrapper<>();
        return R.ok().put("result",moduleFieldDataJsonService.page(new Page<>(pageNum,pageSize), queryWrapper));
    }
    @PostMapping("/add")
    public R add(@RequestBody ModuleFieldDataJson moduleFieldDataJson) {
        return R.ok().put("result",moduleFieldDataJsonService.save(moduleFieldDataJson));
    }
    @PostMapping("/update")
    public R update(@RequestBody  ModuleFieldDataJson moduleFieldDataJson, HashMap<String, Object> updateOptions) {
        QueryWrapper<ModuleFieldDataJson> queryWrapper=new QueryWrapper<>();
        if(null != moduleFieldDataJson.getId() && !("".equals(moduleFieldDataJson.getId()))){
            queryWrapper.eq("id",moduleFieldDataJson.getId());
        }else{
          return R.error("id不能为空");
        }
        return R.ok().put("result",moduleFieldDataJsonService.update(moduleFieldDataJson,queryWrapper));
    }
    
    @GetMapping("/delete")
    public R deleteOne(@RequestParam(value = "id",required = true) String id) {
        return R.ok().put("result", moduleFieldDataJsonService.removeById(id));
    }
}
